﻿using System;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media.Imaging;

namespace WpfAi.Convert
{
    public class ImageConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value.GetType() != typeof(string))
            {
                BitmapImage img = new BitmapImage(new Uri(GlobalEnvironment.NoPicPath, UriKind.RelativeOrAbsolute));// new BitmapImage(new Uri(EnvironmentInfo.NoPicPath, UriKind.Absolute));
                return img;
            }
            string picPath = value.ToString();
            if (!string.IsNullOrWhiteSpace(picPath))
            {
                string path = GlobalEnvironment.PhotoUrl + picPath;
                if (System.IO.File.Exists(path))
                {
                    BitmapImage img = new BitmapImage(new Uri(path, UriKind.Absolute));
                    return img;
                }
                else
                {
                    if (System.IO.File.Exists(path))
                    {
                        BitmapImage img = new BitmapImage(new Uri(path, UriKind.Absolute));
                        return img;
                    }
                    else
                    {
                        BitmapImage img = new BitmapImage(new Uri(GlobalEnvironment.NoPicPath, UriKind.RelativeOrAbsolute));
                        return img;
                    }
                }
            }
            else
            {
                BitmapImage img = new BitmapImage(new Uri(GlobalEnvironment.NoPicPath, UriKind.RelativeOrAbsolute));
                //BitmapImage img = new BitmapImage(new Uri(EnvironmentInfo.NoPicPath, UriKind.Absolute));
                return img;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
